/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Aptech.DAL;

import Aptech.DALInterface.UserInterface;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Users;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;


/**
 *
 * @author nkhoan
 */
public class MgUser implements UserInterface {

    private SessionFactory sessFact;
    private Session sess;
    private Transaction tr;



    @Override
    public void addUser(Users user) {
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();
            sess.save(user);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {

            System.out.println(he.getMessage());

        } finally {
            sess.close();
          
        }

    }

    @Override
    public List getAllUser() {
        List list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(Users.class).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
           
        }

        return list;

    }

    @Override
    public Users getUserByID(String id) {
    	Users user = null;
         try {
             sessFact = Hibernate.HibernateUtil.getSessionFactory();
             sess = sessFact.openSession();
             Criteria criteria = sess.createCriteria(Users.class).add(Restrictions.eq("id", id));
             user = (Users) criteria.uniqueResult();
             sess.flush();
         } catch (HibernateException he) {
             System.out.println(he.getMessage());
         } finally {
             sess.close();
         }
         return user;
    }

    @Override
    public Users  getUserByName(String name){
        
            Users user = new Users();
            List list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
          

            Criteria cr = sess.createCriteria(Users.class);
            cr.add(Restrictions.eq("userName", name));
            list = cr.list();
            if(list.isEmpty()) return null;
            else
            user = (Users) list.get(0);
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
           
        }

        return user;
    }

    @Override
    public void delete(String id) {

        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            String sql = "delete Users where ID = '" + id + "'";
            Query  query = sess.createQuery(sql);
            query.executeUpdate();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
            //sessFact.close();
        }

    }

    @Override
    public void editUser(Users user) {
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();

            sess.update(user);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
           
        }
    }
}
